﻿/****************************************************************
  Author:  Ievgenii Nazaruk, ievgenii.nazaruk@gmail.com 
 License:  Microsoft Public License (Ms-PL)
          ( http://www.opensource.org/licenses/ms-pl.html or
            http://www.microsoft.com/opensource/licenses.mspx )
*****************************************************************/

using System;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Text;

namespace WebLayer
{
    /// <summary>
    /// WebPage provides a base functionalty for requesting html page.
    /// </summary>
    public class WebPage
    {
        private static String CategoryName = "WebPage";

        public Uri PageUri = null;
        public String PageName = null;
        public String PageContent = null;
        
        public static Uri GetActionPageUri(String actionName)
        {
            String str = @"http://sz.speak-up.com.ua/StrefaSluchacza/indexa.php?act={0}";
            String uriStr = String.Format(str, actionName);
            return new Uri(uriStr);
        }
        
        public WebPage(String pageName, Uri pageUri)
        {
            this.PageName = pageName;
            this.PageUri = pageUri;
        }
        
        public virtual void Request()
        {
            Trace.WriteLine(PageName + " - creating request.",CategoryName);
            PageContent = null;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(PageUri);
            
            /* do custom preparations on request */
            OnRequest(request);

            HttpWebResponse response = null;            
            try
            {
                Trace.WriteLine(PageName + " - sending request.", CategoryName); 
                response = (HttpWebResponse)request.GetResponse();

                Trace.WriteLine(PageName + " - got response, processing it.", CategoryName); 
                this.OnResponse(request, response);
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }        
        }

        protected virtual void OnRequest(HttpWebRequest request)
        {
   
        }

        protected virtual void OnResponse(HttpWebRequest request, HttpWebResponse response)
        {
            Trace.WriteLine(PageName + " - response status: "+response.StatusCode,CategoryName);
            PageContent = GetPageContent(response);
        }

        protected virtual string GetPageContent(HttpWebResponse response)
        {
            Stream stream = null;
            StreamReader reader = null;
            String content = null;

            try
            {
                stream = response.GetResponseStream();
                reader = new StreamReader(stream, Encoding.UTF8 );
                content = reader.ReadToEnd();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                else if (stream != null)
                {
                    stream.Close();
                }
            }

            return content;
        }
    }
}
